<?php
namespace app\admin\service;

use app\admin\model\ArticleModel;

class ArticleService
{
    /**
     * 当前Service所对应的Model
     * @var \app\admin\model\ArticleModel
     */
    private $model;

    public function __construct()
    {
        $this->model = new ArticleModel;
    }

    public function index( $param=[], $field="*" )
    {
        $list = $this->model->index( $param, $field );
        foreach ($list as &$val) {
            $val['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
        }
        return [
            'data' => $list,
            'pageSize' => $param['pageSize'],
            'pageNo' => $param['pageNo'],
            'totalPage' => $this->model->dataNum( $param ) / $param['pageSize'],
            'totalCount' => $this->model->dataNum( $param ),
        ];
    }

    public function info( $id )
    {
        $info = $this->model->getInfoByWhere([
            'id' => $id
        ],'
            id,
            title,
            pic,
            create_time
        ');
        $info['create_time'] = date('Y-m-d H:i:s',$info['create_time']);
        return $info;
    }

    public function save($id,$data)
    {
        if( $id==0 ){
            $data['create_time'] = time();
            $res = $this->model->insert($data);
        }else{
            $data['update_time'] = time();
            $res = $this->model->save($data,[
                'id' => $id
            ]);
        }
        return $res;
    }

    public function del( $id )
    {
        $info = $this->model->where([
            'id' => $id
        ])->find();
        $info->is_delete = 2;
        return $info->save();
    }

}